import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/modules/home/widgets/bottom_menu.dart';
import 'package:gczn_hd/app/modules/home/widgets/middle_menu.dart';
import 'package:gczn_hd/app/modules/home/widgets/top_logo.dart';
import 'package:gczn_hd/app/modules/home/widgets/user_list_drawer.dart';

import '../../../core/values/App_img.dart';
import '/app/core/base/base_view.dart';
import '/app/modules/home/controllers/home_controller.dart';

class HomeView extends BaseView<HomeController> {

  HomeView(){
    controller.onViewInit();
  }

  @override
  int get isFullBackground => 0;



  @override
  PreferredSizeWidget? appBar(BuildContext context)  => null;
  @override
  Widget body(BuildContext context) {
    // 获取状态栏高度
    double statusBarHeight = MediaQuery.of(context).padding.top;

    return WillPopScope(
        onWillPop:  () async {
          return await _onWillPop(context);
        },
      child:  Container(
          alignment: Alignment.topLeft,
          padding: EdgeInsets.only(top: statusBarHeight),
          decoration: const BoxDecoration(
            color: AppColors.gcznPrimaryColor,
            image: DecorationImage(
              image: AssetImage(AppImg.homeBackground),
              fit: BoxFit.cover,
              alignment: Alignment.topLeft,
            ),
          ),
          child: Container(
            padding: EdgeInsets.only(left: AppValues.size100.w, right: AppValues.size100.w, top: AppValues.size42.h),
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: [
                TopLogo(),
                SizedBox(height: AppValues.size64.h,),
                MiddleMenu(),
                SizedBox(height: AppValues.size84.h,),
                BottomMenu()
              ],
            ),
          )
      )
    );

  }

  // TODO  按两次退出应该, 未测试
  Future<bool> _onWillPop (BuildContext context) async {

    if ( DateTime.now().difference(controller.lastPressedAt.value) > const Duration(seconds: 2)) {
      controller.lastPressedAt.value = DateTime.now();
      Fluttertoast.showToast(msg: "再按一次退出应用");

      return false; // 不退出
    }
    SystemNavigator.pop();

    return true; // 退出应用
  }

  @override
  Widget? endDrawer() {
    return UserListDrawer();
  }


}
